using System.Collections.Generic;
using MultiplayerServerConnectionHandlers;
using UnityEngine;

public class RacketControlDataReceiver : MonoBehaviour
{
	private class DesiredOrientation
	{
		public Vector3 position;

		public Quaternion rotation;
	}

	public class ReferencePoint
	{
		public RacketCallibrationDB.ReferencePointDefinition.PositionType type;

		public ControllerData message;
	}

	public Vector3 initPosition;

	public Vector3 initRotationEulerAngles;

	public ConnectionHandler myHandler;

	public Quaternion parentRot;

	public Quaternion rotFix;

	public float moveSpeed = 2f;

	public float rotSpeed = 2f;

	public Vector3 racketRotAfter;

	public Vector3 racketRotBefore;

	public Vector3 racketRotInsideAfter;

	public Vector3 racketRotInsideBefore;

	public GameObject racketCallibrationWrapper;

	private List<ReferencePoint> referencePoints = new List<ReferencePoint>();

	private DesiredOrientation desiredOrientation = new DesiredOrientation();

	public bool shouldPollForData;

	private Quaternion racketRotationBase
	{
		get
		{
			return GetQuaternionFromEulerAngleVector(initRotationEulerAngles);
		}
	}

	private Quaternion GetQuaternionFromEulerAngleVector(Vector3 euler)
	{
		Quaternion identity = Quaternion.identity;
		identity.eulerAngles = euler;
		return identity;
	}

	private void OnEnable()
	{
		referencePoints.Clear();
		desiredOrientation.position = initPosition;
		desiredOrientation.rotation = Quaternion.identity;
		desiredOrientation.rotation = racketRotationBase;
		shouldPollForData = false;
	}

	private void OnDisable()
	{
		myHandler = null;
	}

	private ReferencePoint GetReferencePointForType(RacketCallibrationDB.ReferencePointDefinition.PositionType type)
	{
		foreach (ReferencePoint referencePoint in referencePoints)
		{
			if (referencePoint.type == type)
			{
				return referencePoint;
			}
		}
		return null;
	}

	private Quaternion GetAttitudeInMyCoordSystem(Quaternion attitude)
	{
		Quaternion quaternion = Quaternion.FromToRotation(Vector3.forward, Vector3.right) * Quaternion.FromToRotation(Vector3.back, Vector3.up);
		return quaternion * attitude * Quaternion.Inverse(quaternion);
	}

	public void SetReferencePoint(RacketCallibrationDB.ReferencePointDefinition.PositionType type, ControllerData data)
	{
		Debug.Log("Racket - SetReferencePoint");
		Debug.Log("remote orientation: " + data.screenOrientation);
		ReferencePoint referencePoint = GetReferencePointForType(type);
		if (referencePoint == null)
		{
			referencePoint = new ReferencePoint();
			referencePoint.type = type;
			referencePoints.Add(referencePoint);
		}
		referencePoint.message = data;
		RacketCallibrationDB.ReferencePointDefinition definitionForType = RacketCallibrationDB.instance.GetDefinitionForType(type);
		racketCallibrationWrapper.transform.position = definitionForType.position;
		Quaternion quaternionFromEulerAngleVector = GetQuaternionFromEulerAngleVector(definitionForType.rotationEulerAngles);
		Quaternion rotation = QuaternionHelpers.ConvertOrientationBetweenHandedSystems(data.attitude);
		racketCallibrationWrapper.transform.rotation = quaternionFromEulerAngleVector * Quaternion.Inverse(rotation);
		desiredOrientation.rotation = rotation;
		Debug.Log(string.Concat("message: ", data.attitude.eulerAngles, ", destination: ", desiredOrientation.rotation.eulerAngles));
		Debug.Log("attitude: " + data.attitude.eulerAngles);
	}

	public bool HasCallibratedForType(RacketCallibrationDB.ReferencePointDefinition.PositionType type)
	{
		ReferencePoint referencePointForType = GetReferencePointForType(type);
		return referencePointForType != null;
	}

	private void Update()
	{
		base.transform.localRotation = desiredOrientation.rotation;
		if (!shouldPollForData || !myHandler.isActive)
		{
			return;
		}
		bool flag = false;
		for (int num = myHandler.unreadMessages.Count - 1; num >= 0; num--)
		{
			if (myHandler.unreadMessages[num].type == UDPMessageBase.MessageType.ControllerDataMessage)
			{
				ControllerData message = myHandler.unreadMessages[num].GetMessage<ControllerData>();
				if (!flag)
				{
					flag = true;
					ReferencePoint referencePointForType = GetReferencePointForType(RacketCallibrationDB.ReferencePointDefinition.PositionType.PointAtScreen);
					RacketCallibrationDB.ReferencePointDefinition definitionForType = RacketCallibrationDB.instance.GetDefinitionForType(RacketCallibrationDB.ReferencePointDefinition.PositionType.PointAtScreen);
					Quaternion quaternionFromEulerAngleVector = GetQuaternionFromEulerAngleVector(definitionForType.rotationEulerAngles);
					desiredOrientation.rotation = QuaternionHelpers.ConvertOrientationBetweenHandedSystems(message.attitude) * QuaternionHelpers.GetRotFix(message.screenOrientation);
					Debug.Log("Local rotation " + base.transform.localRotation.eulerAngles);
					Debug.Log("Rotation " + base.transform.rotation.eulerAngles);
					Debug.Log("Attitude " + QuaternionHelpers.ConvertOrientationBetweenHandedSystems(message.attitude));
				}
				myHandler.unreadMessages.RemoveAt(num);
			}
		}
	}
}
